package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.stark.pdf.lib.model.IPdfCreateListener;
import com.stark.pdf.lib.model.PdfUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import flc.ast.BaseAc;
import flc.ast.HomeActivity;
import flc.ast.R;
import flc.ast.databinding.ActivityScanPreviewBinding;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.FileP2pUtil;
import stark.common.basic.utils.FileUtil;
import stark.common.basic.utils.RxUtil;

public class ScanPreviewActivity extends BaseAc<ActivityScanPreviewBinding> {

    public static Bitmap sBitmap;

    @Override
    protected int onCreate() {
        return R.layout.activity_scan_preview;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivScanPreviewBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivPdfSave.setOnClickListener(this);
        mDataBinding.ivSave.setOnClickListener(this);

        mDataBinding.imageViewTouch.setImageBitmap(sBitmap);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivPdfSave://转pdf文件
                imgToPdf();
                break;
            case R.id.ivSave://保存图片
                saveImg();
                break;

        }
    }

    @Override
    protected void initData() {

    }

    private void saveImg() {
        showDialog(getString(R.string.saving));
        RxUtil.create(new RxUtil.Callback<String>() {
            @Override
            public void doBackground(ObservableEmitter<String> emitter) {
                //保存到相册
                ImageUtils.save2Album(sBitmap, Bitmap.CompressFormat.PNG);
                //保存到最近文件
                String filePath = FileUtil.generateFilePath(Content.myRecent, ".png");
                ImageUtils.save(sBitmap, filePath, Bitmap.CompressFormat.PNG);
                emitter.onNext("");
            }

            @Override
            public void accept(String result) {
                ToastUtils.showShort(getString(R.string.save_success));
                finish();
                startActivity(HomeActivity.class);
            }
        });
    }

    private void imgToPdf() {
        List<String> list = new ArrayList<>();
        String filePath = FileUtil.generateFilePath(Content.myRecent, ".png");
        ImageUtils.save(sBitmap, filePath, Bitmap.CompressFormat.PNG);
        list.add(filePath);
        PdfUtil.img2Pdf(list, new IPdfCreateListener() {
            @Override
            public void onStart() {
                showDialog(getString(R.string.saving));
            }

            @Override
            public void onEnd(boolean success, String path) {
                FileUtils.delete(filePath);
                if (success) {
//                    String descPath = PathUtils.getRootPathExternalFirst() + "/" + FileUtils.getFileName(path);
//                    FileUtils.copy(path, descPath);
                    //保存到最近文件
                    String filePath = FileUtil.generateFilePath(Content.myRecent, ".pdf");
                    FileUtils.copy(path, filePath);
                    ToastUtils.showShort(getString(R.string.save_success));
                    finish();
                    startActivity(HomeActivity.class);
                } else {
                    ToastUtils.showShort(getString(R.string.save_failure));
                }

            }
        });
    }

}